/*
 DNN Content Builder - www.edablio.com/eportal/ContentBuilder
 Copyright (c) 2004-2005
 by Emerson Lopes (elopes@e-dablio.com) of e-Dablio Consultoria e Projetos Ltda

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

namespace epContentBuilder.UserControls.FieldControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Xml;
	using System.Collections;
	using System.IO;
	using DotNetNuke;

	/// <summary>
	///		Summary description for TextBox.
	/// </summary>
	public class PhotoGalleryDef : System.Web.UI.UserControl
	{
		public const string resource = "~/DesktopModules/epContentBuilder/App_LocalResources/PhotoGalleryDef";

		private System.Xml.XmlDocument xmlDoc = new XmlDocument();
		private string tempString = "";
		private Int32 level = -1;

		protected System.Web.UI.WebControls.Panel idContainer;
		protected System.Web.UI.WebControls.DropDownList ddFolders;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here

			int index = ddFolders.SelectedIndex;

			ddFolders.Items.Clear();

			readDir(System.Web.HttpContext.Current.Server.MapPath("Portals"), ddFolders);			

			ddFolders.SelectedIndex = index;
		}

		public void readDir(string targetDirectory, System.Web.UI.WebControls.DropDownList ddFoldersRef) 
		{
			// Recurse into subdirectories of this directory.
			string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);

			foreach(string subDirectory in subdirectoryEntries)
			{
				level++;

				tempString = subDirectory.Remove(subDirectory.IndexOf(System.Web.HttpContext.Current.Server.MapPath("Portals")),subDirectory.LastIndexOf("\\")+1);

				System.Web.UI.WebControls.ListItem listItem = new System.Web.UI.WebControls.ListItem((tempString.PadLeft(tempString.Length + level, '.')).Replace(".","..."),subDirectory);

				ddFoldersRef.Items.Add(listItem);

				readDir(subDirectory, ddFoldersRef);

				level--;
			}
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		public void SetXml (string extProperties) 
		{
			XmlDocument xml = new XmlDocument();

			XmlNode tempNode;
				
			xml.LoadXml(extProperties);

			tempNode = xml.SelectSingleNode("//SourceFolder");

			if (tempNode == null) 
			{
				XmlDocument tempXml = new XmlDocument();

				tempXml.LoadXml(extProperties);

				tempNode = tempXml.SelectSingleNode("//ExtendedProperties");

				XmlDocument epXml = new XmlDocument();

				epXml.Load(System.Web.HttpContext.Current.Server.MapPath("DesktopModules\\epContentBuilder\\UserControls\\FieldControls\\XML\\PhotoGalleryDef.xml"));

				tempNode.InnerXml = epXml.InnerXml;

				xmlDoc.LoadXml(tempXml.OuterXml);
			} 
			else 
			{
				xmlDoc.LoadXml(extProperties);
			}
			
			tempNode = xmlDoc.SelectSingleNode("//SourceFolder");

			if (tempNode.InnerText != "") 
			{
				ddFolders.SelectedValue = tempNode.InnerText;
			} 
			else 
			{
				ddFolders.SelectedIndex = 0;
			}

			System.Web.UI.Control lastButton = Page.FindControl((string)Session["lastButton"]);

			((ImageButton)lastButton).CommandArgument = xmlDoc.OuterXml;
		}

		public void GetXml (string xml) 
		{
			XmlDocument tempXml = new XmlDocument();

			XmlNode tempNode;
				
			tempXml.LoadXml(xml);

			tempNode = tempXml.SelectSingleNode("//SourceFolder");
			tempNode.InnerText = ddFolders.SelectedValue;
			
			tempNode = tempXml.SelectSingleNode("//UniqueId");
			
			System.Web.UI.Control lastButton = Page.FindControl(tempNode.InnerText);
			
			((ImageButton)lastButton).CommandArgument = tempXml.OuterXml;
		}

	}//class
}//namespace
